package com.elcom.util;

import java.io.FileOutputStream;
import java.io.File;
import java.nio.channels.FileLock;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;

/**
 *
 * @author GiangLT
 */
public class GeneratorSeq {
    //1 khoang Sequence

    /**
     *
     */
    public static final int StepSeq = 1000;
    private static final String fileName = "sequence.seq";
    private static final String filePath = Constant.BASE_FOLDER + "config" + File.separator;
    private static RandomAccessFile file;
    private static int nCurrentSeq; //Sequence doc duoc trong file hien tai
    private static int nSubSeq;

    static {
        File sequenceFile = new File(filePath + fileName);
        if (!sequenceFile.exists()) {
            try {
                sequenceFile.createNewFile();
                FileOutputStream fout = new FileOutputStream(filePath + fileName);
                fout.write("0".getBytes());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        try {
            file = new RandomAccessFile(filePath + fileName, "rw");
            FileChannel channel = file.getChannel();
            FileLock lock = channel.lock();
            String sCurrentSeq = file.readLine();
            nCurrentSeq = Integer.parseInt(sCurrentSeq);
//      System.out.println("Current Sequence in file: "+nCurrentSeq);
            file.seek(0);
            file.setLength(0);
            file.write(((nCurrentSeq + StepSeq) + "").getBytes());
            lock.release();
            file.close();
            channel.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    static GeneratorSeq inst = new GeneratorSeq();
    static byte[] b = new byte[100];
    static String result = "";

    /**
     *
     * @return
     */
    public static synchronized int getNextSeq() {
        if (nSubSeq >= StepSeq) {
            nSubSeq = 0;
            try {
                file = new RandomAccessFile(filePath + fileName, "rw");
                FileChannel channel = file.getChannel();
                FileLock lock = channel.lock();
                String sCurrentSeq = file.readLine();
                nCurrentSeq = Integer.parseInt(sCurrentSeq);
                file.seek(0);
                file.setLength(0);
                file.write(((nCurrentSeq + StepSeq) + "").getBytes());
                lock.release();
                file.close();
                channel.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        nSubSeq++;
        return nSubSeq + nCurrentSeq;
    }

    /**
     *
     * @return GeneratorSeq
     */
    public static GeneratorSeq getInst() {
        return inst;
    }

    /**
     *
     * @param s
     * @param level
     */
    public static void writeLog(String s, int level) {
//    System.out.println(s);
    }
    /**
     *
     * @return true neu khoa file thanh cong
     *         false neu khoa file bi loi
     */
}
